<template>
	<view class="print-set">
		<view class="add-print card pb30" v-if="printerList.length === 0">
			<view class="f28 m30 dis-in">尚未连接任何打印设配</view>
			<view  class="add-link" @click="addPrint">添加蓝牙打印机</view>
		</view>
		<view class="list-print card" v-else>
			<view class="row mb50">
				<view class="col f32 wei">打印机</view>
				<view class="col" @click="addPrint">添加打印机</view>
			</view>
			<view class="row" v-for="item in printerList" :key="item.deviceId">
				<view class="col pinter-info">
					<span class="wei">{{item.name}}</span>
					<span class="f22 p105 " style="margin-left:10rpx;background-color: #F7E9EC;color: #AC5B62;">蓝牙打印机</span>
				</view>
				<view class="col" >
					<view class="close-link fr" @click="closeBLEConnect" :data-id="item.deviceId">断开连接</view>
				</view>
			</view>
		</view>
		<!-- 小票模板 -->
		<view class="module-ticket card">
			<view class="row">
				<view class="col-2 f32 wei">小票模板</view>
				<view class="col">
					<view class="fr">小票模板设置对当前门店所有设备生效</view>
				</view>
			</view>
			<view class="row mt20">
				<view class="col f32 wei">商家联</view>
				<view class="col">
					<view class="fr" style="color: #D0D7E1;" @click="previewImage">预览</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import utils from '@/common/utils.js';
	export default {
		name: 'my-print-set',
		components: {},
		data() {
			return {
				printerList: []
			}
		},
		async onLoad(options) {
			this.util.setNT('打印设置');
		},
		onShow() {
			this.getPrinterList()
			this.setBLEHistoryLink()
			console.log("设备连接列表:", this.BLELinkList)
		},
		computed: mapState(['BLELinkList']),
		methods: {
			...mapMutations(['setBLEHistoryLink']),
			addPrint() {
				this.go({
					t: 1,
					url: `/yb_shd/my/printset/add`
				})
			},
			getPrinterList() {
				this.printerList = []
				this.BLELinkList.map(item => {
					let deviceId = item.split('-%-')[0]
					let name = item.split('-%-')[1]
					this.printerList.push({
						deviceId,
						name
					})
				})
				console.log('断开连接：',this.printerList)
			},
			closeBLEConnect(e) {
				console.log('正在断开连接：',this.printerList)
				uni.closeBLEConnection({
					deviceId: e.currentTarget.dataset.id
				})
				setTimeout(() => {
					this.getPrinterList()
				}, 200)
			},
			// 预览
			previewImage(){
				// uni.previewImage({
				// 	urls:['../../../static/img/print/gz.png']
				// })
				console.log('选择打印机:',this.BLELinkList[0])
				// this.bleSend.receiptTest(this.BLELinkList[0])
			}
		}
	}
</script>

<style lang="scss" scoped>
	.card{
		border-radius: 10rpx;
		padding: 0 30rpx;
		margin: 20rpx;
		background-color: #FFFFFF;
	}
	.add-print {
		text-align: center;
		padding-bottom: 50rpx;
	}
	.list-print{
		padding-top: 50rpx;
		padding-bottom: 30rpx;
	}
	.close-link{
		border: 2rpx solid #ddd;
		border-radius: 58rpx;
		line-height: 50rpx;
		text-align: center;
		font-weight: bold;
		color: #000;
		width: 150rpx;
		height: 58rpx;
		font-size: 24rpx;
	}
	.add-link{
		border: 2rpx solid #ddd;
		border-radius: 70rpx;
		margin: 0 auto;
		line-height: 68rpx;
		text-align: center;
		color: #000;
		width: 230rpx;
		height: 70rpx;
		font-size: 28rpx;
	}
	.pinter-info{
		line-height: 58rpx;
	}
	.module-ticket{
		padding-top: 30rpx;
		padding-bottom: 10rpx;
	}
</style>
